package com.wangrui.client.window;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.wangrui.client.GreetingService;
import com.wangrui.client.GreetingServiceAsync;
import com.wangrui.client.Logsys;

public class LoginWindow extends Window{
		
	public LoginWindow(){
		
		setTitle("Login");
		setAlign(Alignment.CENTER);
		setWidth(400);
		setHeight(200);
		setCanDrag(false);
		setShowMinimizeButton(false);
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();

		final DynamicForm logTimeForm = new DynamicForm();
		logTimeForm.setLayoutAlign(Alignment.CENTER);
		logTimeForm.setTitle("login");
		final TextItem username = new TextItem("CODE", "username");
		final TextItem password = new TextItem("PSW","password");
		
		final ButtonItem login = new ButtonItem("login");
		logTimeForm.setFields(username, password,
				login);
		
		addItem(logTimeForm);
		
		login.addClickHandler(new ClickHandler(){
			//
			 @Override
			 public void onClick(ClickEvent event) {
			
			
			
			 String id = (String) username.getValue();
			 String psw = (String) password.getValue();
			
			
			 GreetingServiceAsync loginService =
			 GreetingService.Util.getInstance();
			
			 AsyncCallback cb_login = new AsyncCallback() {
			
			 @Override
			 public void onFailure(Throwable caught) {
			 // TODO Auto-generated method stub
			
			 }
			
			 @Override
			 public void onSuccess(Object result) {
			 // TODO Auto-generated method stub
			 System.out.println(result.toString()+"********");
			
			 Boolean ok = (Boolean) result;
			
			 if (ok.booleanValue()) {
			
			 SC.say("login sucess!");
	 
			 }
			 else
			 SC.say("login failed");
			 }
			 };
			
			 loginService.login(id, psw, cb_login);
			
			 }
			
			 });
		
		
	}
	private native void applyEffect(String effect, Element element)/*-{
	
	var ne = $wnd._nativeExtensions;
	
	$wnd._nativeExtensions = false;

    $wnd.Effect[effect](element);
    
    $wnd.__nativeExtensions = ne;
}-*/;

}
